/*******************************************************
> Copyright (C) 2024 ==iot== All rights reserved.
> File Name: 4.c
> Author: wyf
> Created Time: 2024年09月02日 星期一 18时57分14秒
***************************************************/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//统计一个整数二进制位1的个数
#define MAX_LENGTH 32  
int count_binaray_bits(int num) {
    int count = 0;

    while (num) {
        if (num & 1) {
            count++;
        }
        num >>= 1;
    }

    return count;
}

int decimalism_to_bin(int number, int* arr)
{
    int i = 0;

    while (number > 0)
    {
        arr[i] = number % 2;
        number /= 2;
        i++;
    }

    return i;
}
int main(int argc, const char* argv[])
{
    printf("请输入一个数：\n");
    int num;
    int arr[32] = { 0 };
    int res = 0;
    scanf("%d", &num);
    printf("该数的二进制为：\n");
    int bit = decimalism_to_bin(num, arr);

    for (int i = bit - 1; i >= 0; i--)
    {
        printf("%d", arr[i]);
    }

    printf("\n");
    res = count_binaray_bits(num);
    printf("该数的二进制中1的个数为：%d\n", res);




    return 0;

}


